﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lizk.SimpleHUD;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using Lizk.Utilities;
using SharpMedia.Input;
using SharpMedia.Properties;

namespace SharpMedia
{
    public partial class SettingsForm : Form
    {
        bool dirty = false;
        InputSettings inputSettings;
        string inputFile;
        public SettingsForm()
        {
            Icon = Resources.screen;
            InitializeComponent();
            txtDaemonDrive.Text = SharpMedia.Settings.MountDrive;
            txtDaemonArgs.Text = SharpMedia.Settings.MountProgramArguments;
            txtDaemonPath.Text = SharpMedia.Settings.MountProgramPath;

            inputFile = Path.Combine(SharpMedia.DataDirectory, "input.xml");

            dirty = false;
            if (!InputSettings.TryLoadSettings(inputFile, out inputSettings))
                dirty = true;

            foreach (var item in inputSettings.Bindings)
            {
                ListViewItem lvi = new ListViewItem(new string[] { item.Command.ToString(), item.PrimaryString, item.SecondaryString });
                lvi.Tag = item;
                listView1.Items.Add(lvi);
            }
            foreach (var item in SharpMedia.Settings.QuickLinks)
            {
                ListViewItem lvi = new ListViewItem(new string[] { item.Title, item.Subtitle, item.Image, item.Directory });
                lvi.Tag = item;
                listView2.Items.Add(lvi);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetKey();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            SetKey();
        }

        private void SetKey()
        {
            if (listView1.SelectedItems.Count != 1)
                return;

            KeyboardBinding<HUDCommands> command = (KeyboardBinding<HUDCommands>)listView1.SelectedItems[0].Tag;
            
            KeyInputForm f = new KeyInputForm(command);
            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog(this);

            listView1.SelectedItems[0].SubItems[1].Text = command.PrimaryString;
            listView1.SelectedItems[0].SubItems[2].Text = command.SecondaryString;
            dirty = true;
        }

        private void listView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SetKey();
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (dirty)
            {
                DialogResult result = MessageBox.Show("Save Changes?", "Unsaved Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    Save();
                }
                else if (result == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }
        private void Save()
        {
            SharpMedia.Settings.MountProgramPath = txtDaemonPath.Text;
            SharpMedia.Settings.MountProgramArguments = txtDaemonArgs.Text;
            SharpMedia.Settings.MountDrive = txtDaemonDrive.Text;
            XmlSettings.saveXml(inputSettings, inputFile);
            XmlSettings.saveXml(SharpMedia.Settings, Path.Combine(SharpMedia.DataDirectory, "settings.xml"));
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dirty = true;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (Directory.Exists(txtQuickLinkPath.Text))
                fbd.SelectedPath = txtQuickLinkPath.Text;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtQuickLinkPath.Text = fbd.SelectedPath;
            }
        }
        QuickLink selectedQuickLink = null;
        ListViewItem selectedQuickLinkItem = null;
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            groupBox3.Enabled = listView2.SelectedIndices.Count == 1;
            if (listView2.SelectedIndices.Count == 1)
            {
                selectedQuickLink = listView2.SelectedItems[0].Tag as QuickLink;
                selectedQuickLinkItem = listView2.SelectedItems[0];
                txtQuickLinkImage.Text = selectedQuickLink.Image;
                txtQuickLinkPath.Text = selectedQuickLink.Directory;
                txtQuickLinkSubtitle.Text = selectedQuickLink.Subtitle;
                txtQuickLinkTitle.Text = selectedQuickLink.Title;
            }
            else
                selectedQuickLink = null;


            toolStripButton2.Enabled = listView2.SelectedIndices.Count > 0;
            toolStripButton3.Enabled = listView2.SelectedIndices.Count == 1;
            toolStripButton4.Enabled = listView2.SelectedIndices.Count == 1;
        }

        private void txtQuickLinkPath_TextChanged(object sender, EventArgs e)
        {
            if (selectedQuickLink == null && selectedQuickLink.Directory != txtQuickLinkPath.Text)
                return;

            selectedQuickLink.Directory = txtQuickLinkPath.Text;
            selectedQuickLinkItem.SubItems[3].Text = txtQuickLinkPath.Text;
            dirty = true;
        }

        private void txtQuickLinkImage_TextChanged(object sender, EventArgs e)
        {
            if (selectedQuickLink == null && selectedQuickLink.Image != txtQuickLinkImage.Text)
                return;
            selectedQuickLink.Image = txtQuickLinkImage.Text;
            selectedQuickLinkItem.SubItems[2].Text = txtQuickLinkImage.Text;
            dirty = true;

        }

        private void txtQuickLinkSubtitle_TextChanged(object sender, EventArgs e)
        {
            if (selectedQuickLink == null && selectedQuickLink.Subtitle != txtQuickLinkSubtitle.Text)
                return;
            selectedQuickLink.Subtitle = txtQuickLinkSubtitle.Text;
            selectedQuickLinkItem.SubItems[1].Text = txtQuickLinkSubtitle.Text;
            dirty = true;

        }

        private void txtQuickLinkTitle_TextChanged(object sender, EventArgs e)
        {
            if (selectedQuickLink == null && selectedQuickLink.Title != txtQuickLinkTitle.Text)
                return;
            selectedQuickLink.Title = txtQuickLinkTitle.Text;
            selectedQuickLinkItem.SubItems[0].Text = txtQuickLinkTitle.Text;
            dirty = true;

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            QuickLink ql = new QuickLink(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Documents", "My Documents", "folder.png");
            SharpMedia.Settings.QuickLinks.Add(ql);
            ListViewItem lvi = new ListViewItem(new string[] { ql.Title, ql.Subtitle, ql.Image, ql.Directory });
            lvi.Tag = ql;
            listView2.Items.Add(lvi);
            foreach (ListViewItem item in listView2.Items)
            {
                item.Selected = item == lvi;
            }
            dirty = true;

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView2.SelectedItems)
            {
                SharpMedia.Settings.QuickLinks.Remove(item.Tag as QuickLink);
                listView2.Items.Remove(item);
            }
            dirty = true;

            
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            int index = listView2.SelectedIndices[0];
            index = ((--index) + listView2.Items.Count) % listView2.Items.Count;
            QuickLink ql = SharpMedia.Settings.QuickLinks[listView2.SelectedIndices[0]];
            SharpMedia.Settings.QuickLinks.Remove(ql);
            SharpMedia.Settings.QuickLinks.Insert(index, ql);
            ListViewItem i = listView2.Items[listView2.SelectedIndices[0]];
            i.Remove();
            listView2.Items.Insert(index, i);
            dirty = true;

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            int index = listView2.SelectedIndices[0];
            index = ((++index) + listView2.Items.Count) % listView2.Items.Count;
            QuickLink ql = SharpMedia.Settings.QuickLinks[listView2.SelectedIndices[0]];
            SharpMedia.Settings.QuickLinks.Remove(ql);
            SharpMedia.Settings.QuickLinks.Insert(index, ql);

            ListViewItem i = listView2.Items[listView2.SelectedIndices[0]];
            i.Remove();
            listView2.Items.Insert(index, i);
            dirty = true;

        }

    }



}
